// pages/integral/integral.js
const app = getApp();
const utils = require('../../utils/util.js');
const post = utils.post;
const getCurrentPageUrlWithArgs = utils.getCurrentPageUrlWithArgs;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        jlist: [],
        m_uid: '',
        jf: "",
        show_jf: '',
        show_jf50: '',
        weapp_status: 0
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        let that = this
        if (options.show_jf == 1) {
            that.setData({
                show_jf50: 100,
                show_jf: options.show_jf
            });
        } else {
            that.setData({
                show_jf50: 0,
                show_jf: options.show_jf
            });
        }
        //console.log(options.show_jf)
        let p0 = {
            m: 'weapp_status',
            v: 3
        };
        post(p0).then(res => {
            that.setData({
                weapp_status: res.status
            });

            if (1 == that.data.weapp_status) { // 审核通过了 
                wx.showToast({
                    icon: 'loading',
                    duration: 500
                });
                that.doGetUserInfo(options);
            }
        });

    },
    doGetUserInfo(options) {
        let that = this;
        var _userinfo = wx.getStorageSync('_userinfo');
        var weapp_openid = wx.getStorageSync('weapp_openid');
        if (_userinfo && weapp_openid && weapp_openid == _userinfo.openId) {
            that.doLogin(options);
        } else {
            // 获取用户信息
            wx.login({
                success(res) {
                    // console.log(res);

                    var code = res.code;
                    wx.getUserInfo({
                        success(res2) {
                            // console.log(res2)
                            var encryptedData = res2.encryptedData;
                            var iv = res2.iv;
                            var p = {
                                m: 's_get_userinfo',
                                code: code,
                                encryptedData: encryptedData,
                                iv: iv
                            };
                            post(p).then(res3 => {
                                if (0 == res3.r) {
                                    // console.log(res3);
                                    var userinfo = res3.userinfo;
                                    var weapp_openid = userinfo.openId;
                                    wx.setStorageSync('weapp_openid', weapp_openid); // 桔子派的openId
                                    wx.setStorageSync('_userinfo', res3.userinfo);
                                    wx.setStorageSync('device_info', wx.getSystemInfoSync());

                                    wx.showToast({
                                        title: '登录成功',
                                        icon: 'none'
                                    });

                                    that.doLogin(options);
                                }
                            })
                        }
                    })
                }
            })
        }
    },
    doLogin(params) {
        let that = this;
        var z_uid = wx.getStorageSync("j_uid"); // 对应赚客login_id
        var m_uid = wx.getStorageSync("jm_uid"); // 对应赚客用户uid
        if (z_uid && m_uid) {
            that.onShow();
        } else {
            let p = {};
            var channel = params.channel; //渠道号
            if (!channel) {
                wx.showModal({
                    content: '渠道为空，无法使用',
                    showCancel: false
                });

                return;
            }
            var third_uid = params.third_uid; //用户的uid
            if (!third_uid) {
                wx.showModal({
                    content: '第三方用户uid为空，无法使用',
                    showCancel: false
                });

                return;
            }

            p = {
                m: 'j_login',
                channel: channel,
                third_uid: third_uid,
            };

            var _userinfo = wx.getStorageSync('_userinfo');
            var weapp_openid = wx.getStorageSync('weapp_openid');
            if (_userinfo && weapp_openid && weapp_openid == _userinfo.openId) {
                p.userinfo = JSON.stringify(_userinfo);
            }
            post(p).then(res => {
                //console.log(res)
                if (0 == res.r) {
                    wx.setStorageSync("j_uid", res.uid); // 对应赚客手机号
                    wx.setStorageSync("jm_uid", res.m_uid); // 对应赚客用户uid
                    that.onShow();
                }
            });
        }
    },

    tapId(event) {
        let that = this;
        let url = '/pages/skip/skip?uid=' + that.data.m_uid + '&weappid=' + event.target.dataset.weappid + '&from=jlist';
        wx.navigateTo({
            url: url
        });
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        let that = this;
        var z_uid = wx.getStorageSync("j_uid"); // 对应赚客手机号
        var m_uid = wx.getStorageSync("jm_uid"); // 对应赚客用户uid
        if (z_uid && m_uid) {
            that.setData({
                z_uid: z_uid,
                m_uid: m_uid
            })
            that.listData()
        }
    },

    listData() {
        let that = this;
        let p = {
            m: 'jindex',
            juid: that.data.z_uid
        };
        post(p).then(res => {
            //console.log(res)
            that.setData({
                m_uid: res.m_uid,
                jf: res.jf,
                jlist: res.jlist
            });
        })
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    /*onShareAppMessage: function() {

    },*/
    submitFormId(e) {
        // console.log(e);
        let formId = e.detail.formId;
        let button = e.detail.target.dataset.name;
        let page = getCurrentPageUrlWithArgs();
        let that = this;
        let p = {
            m: 'colform',
            uid: that.data.m_uid,
            openid: wx.getStorageSync('weapp_openid'),
            formId: formId
        };
        if (page && page.length > 0) {
            p.page = page;
        }
        if (button && button.length > 0) {
            p.button = button;
        }
        // console.log(p)
        post(p).then(res => {})
    }
})